<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- <van-cell v-for="item in postDataList" :key="item.id">
          <Post :postItem="item"></Post>
        </van-cell> -->
        <van-cell v-for="item in list" :key="item">
          <Post :postItem="item" @refresh="onRefresh"></Post>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup lang="ts">
import Post from '@/components/post/post.vue'
import { computed, reactive, ref, toRefs, onMounted } from 'vue'
import {
  findPost,
  findPostByUserId,
  findPostByTopicName,
  findPostByUserIdList,
} from '@/api/service'
const props = defineProps<{
  postType?: number
  userId?: string
  topicName?: string
}>()
console.log(props)

const followList = ref([])
const list = ref([])
const loading = ref(false)
const finished = ref(false)
const refreshing = ref(false)
let i = 1
const onLoad = async () => {
  if (refreshing.value) {
    list.value = []
    refreshing.value = false
    i = 1
  }
  let res: any
  let total = 0

  if (props.userId != undefined) {
    console.log('props.userId', props.userId)
    res = await findPostByUserId({
      userId: props.userId,
      page: i,
      pageSize: 2,
    })
    list.value.push(...res.data.data)
    total = res.data.total


  } else if (props.topicName != undefined) {
    console.log('props.topicName', props.topicName)
    res = await findPostByTopicName({
      topic: props.topicName,
      page: i,
      pageSize: 2,
    })
    list.value.push(...res.data.data)
    total = res.data.total


  } else {
    console.log('props中既没有userId也没有topickName')
    console.log(followList.value)
    if (followList.value.length > 0) {
      res = await findPostByUserIdList({
        userIdList:followList.value,
        page: i,
        pageSize: 2,
      })
      list.value.push(...res.data.data)
      total = res.data.total
    } else {
      res = await findPost({ page: i, pageSize: 2 })
      list.value.push(...res.data.data)
      total = res.data.total
    }
 
  }

  i++
  loading.value = false

  if (list.value.length >= total) {
    finished.value = true
  }
}

onMounted(() => {
  setTimeout(() => {
    // onRefresh()
  }, 500);

})
const onRefresh = () => {
  console.log('diaoyongle refresh')

  // 清空列表数据
  finished.value = false

  // 重新加载数据
  // 将 loading 设置为 true，表示处于加载状态
  loading.value = true
  refreshing.value = true
  // i=1
  onLoad()
}

defineExpose({
  onRefresh,
  followList,
})
</script>

<style scoped></style>
